package com.alibaba.cloud.base.business.dict.service.impl;

import com.alibaba.cloud.base.business.dict.dao.BasDictTypeMapper;
import com.alibaba.cloud.base.business.dict.domain.BasDictType;
import com.alibaba.cloud.base.business.dict.service.BasDictTypeService;
import com.alibaba.cloud.base.constant.BaseConstant;
import com.alibaba.fastjson.JSON;
import com.alibaba.framework.exception.BusinessException;
import com.alibaba.framework.wrapper.RedisWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class BasDictTypeServiceImpl implements BasDictTypeService {

    @Autowired
    private BasDictTypeMapper basDictTypeMapper;

    @Autowired
    private RedisWrapper<String, Object> redisWrapper;

    @Override
    public List<BasDictType> getAll() {

        QueryWrapper<BasDictType> wrapper = new QueryWrapper<>();
        List<BasDictType> basDictList = basDictTypeMapper.selectList(wrapper);
        return basDictList;
    }

    @Override
    public List<BasDictType> getDictTypeListByType(String typeCode) {
        String typeCodeKey = BaseConstant.BASE_DICT_TYPE_KEY + typeCode;
        if (redisWrapper.hasKey(typeCodeKey)) {
            Object value = redisWrapper.getValue(typeCodeKey);
            List<BasDictType> basDictTypeList = JSON.parseArray(value.toString(), BasDictType.class);
            return basDictTypeList;
        }

        QueryWrapper<BasDictType> wrapper = new QueryWrapper<>();
        wrapper.eq("type_code", typeCode);
        List<BasDictType> basDictTypeList = basDictTypeMapper.selectList(wrapper);
        String basDictListJSON = JSON.toJSONString(basDictTypeList);
        redisWrapper.putValueWithExpireTime(typeCodeKey, basDictListJSON, BaseConstant.EXPIRE_TIME, TimeUnit.SECONDS);
        return basDictTypeList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(BasDictType basDictType) {
        validateDictType(basDictType.getTypeName());
        basDictType.setCreateTime(new Date());
        basDictTypeMapper.insert(basDictType);
        redisWrapper.delWithKey(BaseConstant.BASE_DICT_TYPE_KEY);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(BasDictType basDictType) {
        validateDictType(basDictType.getTypeName());
        basDictType.setUpdateTime(new Date());
        basDictTypeMapper.updateById(basDictType);
        redisWrapper.delWithKey(BaseConstant.BASE_DICT_TYPE_KEY);
    }

    /**
     * @descript 校验字典类型名称是否重复
     * @author focus
     * @date 2025/10/27
     */
    private void validateDictType(String typeName) {
        QueryWrapper<BasDictType> wrapper = new QueryWrapper<>();
        wrapper.eq("type_name", typeName);
        BasDictType dict = basDictTypeMapper.selectOne(wrapper);
        if (!Objects.isNull(dict)) {
            throw new BusinessException("字典类型项目已存在");
        }
    }
}
